package is202.hrms.web;

import is202.hrms.ejb.FeedbackEJB;
import is202.hrms.ejb.ModulenEJB;
import is202.hrms.ejb.ProgressEJB;
import is202.hrms.ejb.UserEJB;
import is202.hrms.entity.Feedback;
import is202.hrms.entity.Progid;
import is202.hrms.entity.Progress;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * The backing bean for feedbackartment.xhtml. The Named annotation marks it
 * as a CDI backing bean. The ConversationScoped annotation controls
 * the lifecycle of the bean. The lifetime of a ConversationScoped bean is
 * explicitly managed in the code.
 *
 * @author even
 */
@Named("feedbackbean")
@RequestScoped
public class FeedbackBean implements Serializable {

    // The @Inject annotation is a request to the server to
    // give us a Conversation object, and store it in conv

    @EJB FeedbackEJB feedbackEjb;
    @EJB ProgressEJB progressEjb;
    @EJB
    UserEJB userEjb;
    @EJB
    ModulenEJB modulenEjb;
    private Feedback feedback;
    private boolean updating;
    private boolean approved;

    // Feedback properties - These properties will be copied to a Feedback
    // object, when the time comes to save the data in the database.
    private int feedbackId;
    private String message;
    private List<Feedback> feedbacks;
    private Progress progress;

    public FeedbackBean() {
    }

    public Progress getProgress() {
        return progress;
    }

    public void setProgress(Progress progress) {
        this.progress = progress;
    }

    public boolean isApproved() {
        return approved;
    }

    public void setApproved(boolean approved) {
        this.approved = approved;
    }
    
    
    /**
     * called when the feedback selects a feedbackartment from the list in
     * feedbackartments.xhtml.
     */
    public void setFeedbackId(int feedbackId) {
       
        
        feedback = feedbackEjb.find(feedbackId);
        if (null == feedback) {
            // we will get here if feedbackId is not a valid, or if
            // feedbackId is valid but there is no feedbackartment with that feedbackno
            updating = false;
            feedbackId = 0;
           // name = "";
         //   employees = new ArrayList<Employee>();
        }
        else {
            updating = true;
            this.feedbackId = feedback.getFeedbackId();
            message = feedback.getMessage();
            approved = feedback.isApproved();
            
          //  employees = feedback.getEmployees();
        }
       
    }
    
    public void progressFeedback(int studentId, int modulenId){
        
        Progid p = new Progid(studentId, modulenId);

        progress = progressEjb.find(p);
        progress.setFeedback(feedback);
        progress.setApproved(approved);
        
        if(approved){
            //Progress prog = new Progress();
            //prog.setBruker(userEjb.find(studentId));
           
           // prog.setModuler(modulenEjb.find(151));
           // progressEjb.insert(prog);
        }
    
    }

    
    public int getFeedbackId() {
        return feedbackId;
    }

    public FeedbackEJB getFeedbackEjb() {
        return feedbackEjb;
    }

    public void setFeedbackEjb(FeedbackEJB feedbackEjb) {
        this.feedbackEjb = feedbackEjb;
    }

    public Feedback getFeedback() {
        return feedback;
    }

    public void setFeedback(Feedback feedback) {
        this.feedback = feedback;
    }

    public boolean isUpdating() {
        return updating;
    }

    public void setUpdating(boolean updating) {
        this.updating = updating;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Feedback> getFeedbacks() {
        return feedbacks;
    }

    public void setFeedbacks(List<Feedback> feedbacks) {
        this.feedbacks = feedbacks;
    }
    
    

    private Feedback createFeedbackObject() {
        // Create a feedbackartment object containing the data
        // from the form. The object can be passed on to
        // feedbackListBean
        feedback = new Feedback();
        feedback.setFeedbackId(feedbackId);
        feedback.setMessage(message);
        feedback.setApproved(approved);
        
        return feedback;
    }

    public String saveFeedback(int stud, int mod) {
         feedback = createFeedbackObject();
         progressFeedback(stud, mod);
        if (feedback.getFeedbackId() > 0) feedbackEjb.update(feedback);
        else feedbackEjb.insert(feedback);
        progressEjb.update(progress);
      
        return "feedback.xhtml";
    }
    

    public View deleteFeedback() {
        feedback = createFeedbackObject();
        feedbackEjb.delete(feedback);


        return View.modulchange;
    }
    
    public String getApprovement(){
        if (feedback == null){
        return "Ikke vurdert";
        }
        if(approved){
            return "Godkjent";
            
        }
        else{
            return "Ikke godkjent"; 
                    
        }
        
        
        
    }
    
    
}
